#pragma once

#include "global.h"
#include "map.h"
#include "piece.h"
#include "templatevector.h"

class Game
{
private:
	Map m_map;
	Piece m_piece;
	bool m_running;
	time_t m_timer[2];
	int m_player;
	bool m_startGame;
	int p0;
	int p1;
	int boardColor;
	bool m_movingPiece;
	bool m_validOldLocation;
	int m_originalX;
	int m_originalY;
	int m_originalIcon;
	int m_originalColor;

	TemplateVector<char> player0_capturedPieces;
	TemplateVector<char> player1_capturedPieces;
public:
	Game()
	{
		//m_map = 0;
		//m_piece = 0;
		m_running = true;
		m_timer[0] = 0;
		m_timer[1] = 0;
		m_player = 0;
		m_startGame = false;
		p0 = 10;
		p1 = 9;
		boardColor = 12;
		m_movingPiece = false;
		m_validOldLocation = false;
		m_originalX = 0;
		m_originalY = 0;
		m_originalIcon = 0;
		m_originalColor = 0;

	}
	bool isRunning(){return m_running;}

	void initBoard()
	{
		//i probably didnt do this part as well as i should have
		//i r g b
		//8 4 2 1

		// r t b K Q b t r
		// 0 1 2 3 4 5 6 7

		m_map.load("board.txt");
		
		//this is for player 0
		//king
		m_map.set(3, 0, 'K', p0);

		//queen
		m_map.set(4, 0, 'Q', p0);
		
		//rook
		m_map.set(0, 0, 'r', p0);
		m_map.set(7, 0, 'r', p0);

		//bishop
		m_map.set(2, 0, 'b', p0);
		m_map.set(5, 0, 'b', p0);

		//knight
		m_map.set(1, 0, 't', p0);
		m_map.set(6, 0, 't', p0);

		//pawn
		for(int x = 0; x < m_map.getWidth(); ++x)
			m_map.set(x, 1,'p',p0);
		
		//p1
		//king
		m_map.set(3, 7, 'K', p1);

		//queen
		m_map.set(4, 7, 'Q', p1);

		//rook
		m_map.set(0, 7, 'r', p1);
		m_map.set(7, 7, 'r', p1);

		//bishop
		m_map.set(2, 7, 'b', p1);
		m_map.set(5, 7, 'b', p1);

		//knight
		m_map.set(1, 7, 't', p1);
		m_map.set(6, 7, 't', p1);

		//pawn
		for(int x = 0; x < m_map.getWidth(); ++x)
			m_map.set(x, 6,'p',p1);
	}

	bool init()
	{
		//printing out some rules and what things are
		setConsoleCursor(0,10);
		printf("k to start the game\n");
		printf("[Space] to select piece\n");
		printf("p to set piece down\n");
		printf("= to put the piece back\n");
		printf("~ to forfeit\n");//tilda key shift + the key, so its harder to accidently forfeit
		printf("w a s d to move the piece or cursor\n");
		printf("q e z c to move the piece or cursor\n");
		printf("\np = Pawn    r = Rook\n");
		printf("t = Knight  b = Bishop\n");
		printf("K = King    Q = queen\n");

		initBoard();

		m_piece.setOwner(0);
		m_piece.setColor(p0);
		return true;
	}

	void forfeit()
	{
		//if you decide to give up, the other player wins
		setConsoleCursor(0,9);
		int temp;
		switch(m_player)
		{
		case 0:	temp = p1;	break;
		case 1:	temp = p0;	break;
		}
		setConsoleColor(temp);
		switch(m_player)
		{
		case 0:	printf("Player 2 wins!");	break;
		case 1:	printf("Player 1 wins!");	break;
		}
		m_running = false;
	}
	void draw()
	{
		m_map.draw();
		m_piece.draw();
		//setConsoleCursor(0,15);
		//printf("x: %d, y: %d\n", m_piece.getX(), m_piece.getY());
	}

	void selectPiece()
	{	
		//setConsoleCursor(20,20);
		//printf("color: %d", originalColor);
		
		//arent already moving a piece...tho this might not work
		//but dont try to select an empty spot
		if(!m_movingPiece && m_map.get(m_piece.getX(), m_piece.getY()) != '.') 
		{
			int temp;
			switch(m_player)
			{
			case 0:	temp = p0;	break;
			case 1: temp = p1;	break;
			}

			//setConsoleCursor(0,12);
			//printf("piece owner: %d, m_player: %d", m_piece.getOwner(), m_player);

			//to make sure you move your own piece
			//if(m_piece.getOwner() == m_player)
			//setConsoleCursor(0,20);
			//printf("piece: %d, map: %d", m_piece.getColor(),m_map.getColor(m_piece.getX(), m_piece.getY()));
			if(m_piece.getColor() == m_map.getColor(m_piece.getX(), m_piece.getY()))
			{
				//setConsoleCursor(20,21);
				//printf("%d", m_map.getColor(m_piece.getX(), m_piece.getY()));
				m_piece.setColor(temp);
				m_piece.setIcon(m_map.get(m_piece.getX(), m_piece.getY()));

				m_map.set(m_piece.getX(), m_piece.getY(), '.', boardColor);
				m_movingPiece = true;
			}
			m_originalX = m_piece.getX();
			m_originalY = m_piece.getY();
			//m_originalIcon = m_map.get(m_piece.getX(), m_piece.getY());
			//m_originalColor = m_map.getColor(m_piece.getX(), m_piece.getY());
			m_validOldLocation = true;
		}
	}

	void putPieceBack()
	{
		//so you can put the piece back if you decide to move a different piece
		//before you could pick up a piece, then put it back down for your turn
		if(m_validOldLocation)
		{
			m_piece.setX(m_originalX);
			m_piece.setY(m_originalY);
			m_map.set(m_piece.getX(), m_piece.getY(),m_piece.getIcon(), m_piece.getColor());
			m_piece.setIcon(1);
			m_validOldLocation = false;
			m_movingPiece = false;
		}
	}

	void endTurn()
	{
		//switches the  player
		if(m_player == 0)
		{
			m_player = 1;
			m_piece.setOwner(1);
			m_piece.setColor(p1);
		}
		else
		{
			m_player = 0;
			m_piece.setOwner(0);
			m_piece.setColor(p0);
		}
		m_piece.setIcon(1);
		m_movingPiece = false;
		m_validOldLocation = false;
	}

	void setPieceOnBoard()
	{
		if(m_validOldLocation && m_piece.getX() == m_originalX  && m_piece.getY() == m_originalY)
			putPieceBack();

		//for now, just moving the pieces to empty spaces
		//no limits to how far yet
		if(m_map.get(m_piece.getX(), m_piece.getY()) == '.' && m_piece.getIcon() != 1)
		{
			//setConsoleCursor(15,15);
			//printf("Setting Piece");
			m_map.set(m_piece.getX(), m_piece.getY(),m_piece.getIcon(), m_piece.getColor());
			endTurn();
		}

		//if the spot is the other player's and not the board
		//setConsoleCursor(20,15);
		//printf("map color: %d, piece color: %d",m_map.getColor(m_piece.getX(), m_piece.getY()), m_piece.getColor());
		if(m_map.getColor(m_piece.getX(), m_piece.getY()) != m_piece.getColor()
			&& m_map.getColor(m_piece.getX(), m_piece.getY()) != boardColor
			&& m_piece.getIcon() != 1)
		{
			//if it is the king they capture, then someone wins
			if(m_map.get(m_piece.getX(), m_piece.getY()) == 'K')
			{
				int temp;
				switch(m_player)
				{
				case 0:	temp = p0;	break;
				case 1:	temp = p1;	break;
				}
				setConsoleCursor(0,8);
				setConsoleColor(temp);
				switch(m_player)
				{
				case 0:	printf("Player 1 Wins!\n");	break;
				case 1:	printf("Player 2 Wins!\n");	break;
				}
				m_running = false;
			}
			//it still adds the king to the list of captured pieces
			//but if its not the king, it still adds them.
			switch(m_player)
			{
			case 0: player0_capturedPieces.add(m_map.get(m_piece.getX(), m_piece.getY())); break;
			case 1: player1_capturedPieces.add(m_map.get(m_piece.getX(), m_piece.getY())); break;
			}
			m_map.set(m_piece.getX(), m_piece.getY(),m_piece.getIcon(), m_piece.getColor());
			
			endTurn();
		}
	}
	
	void handleInput(int input)
	{
		//handling the input from the keyboard
		//these 2 are out here so you can actually start the game or quit before you start the game
		if(input == 'k')
			m_startGame = true;
		if(input == 27)
			m_running = false;
		if(m_startGame)
		{
			switch(input)
			{
			case 'w':	m_piece.addNextMove(0,-1);	break;
			case 'a':	m_piece.addNextMove(-1,0);	break;
			case 's':	m_piece.addNextMove(0,1);	break;
			case 'd':	m_piece.addNextMove(1,0);	break;
			case 'q':	m_piece.addNextMove(-1,-1);	break;
			case 'e':	m_piece.addNextMove(1,-1);	break;
			case 'z':	m_piece.addNextMove(-1,1);	break;
			case 'c':	m_piece.addNextMove(1,1);	break;
			case ' ':	selectPiece();				break;
			case 'p':	setPieceOnBoard();			break;
			case '~':	forfeit();					break;
			case '=':	putPieceBack();				break;
			//case 'm':	initBoard();				break;
			//case 'o':	endTurn();					break;
			//case 'k':	m_startGame = true;			break;
			//case 27:	m_running = false;			break;
			default:								break;
			}
		}
	}
	bool update(time_t ms)
	{
		if(m_startGame)
		{
			m_timer[m_player] += ms;
			int oldx = m_piece.getX();
			int oldy = m_piece.getY();

			//update the m_piece since input is separate
			m_piece.update();

			//keeping m_pieces on the m_map
			if(m_piece.getX() < 0 || m_piece.getY() < 0
			|| m_piece.getX() >= m_map.getWidth()
			|| m_piece.getY() >= m_map.getHeight())
			{
				m_piece.setX(oldx);
				m_piece.setY(oldy);
			}

			//making it so the pieces cant go over each other, unless its the knight, or the selection cursor
			if(m_piece.getIcon() != 't' && m_piece.getIcon() != 1 && m_map.get(m_piece.getX(), m_piece.getY()) != '.'
				&& m_piece.getColor() == m_map.getColor(m_piece.getX(), m_piece.getY()))
			{
				m_piece.setX(oldx);
				m_piece.setY(oldy);
			}

		}

		//print out the player and timer for them
		setConsoleCursor(10,0);
		printf("Player %d's Turn", m_player + 1);
		setConsoleCursor(10,1);
		setConsoleColor(p0);
		printf("Player %d: %d", 1, m_timer[0] / 1000);
		setConsoleCursor(10,2);
		setConsoleColor(p1);
		printf("Player %d: %d", 2, m_timer[1] / 1000);

		//print out captured pieces for player 0
		if(player0_capturedPieces.size() != 0)
		{
			setConsoleCursor(10, 4);
			setConsoleColor(p0);
			printf("Player 1 captured: ");
			for(int i = 0; i < player0_capturedPieces.size(); ++i)
			{
				setConsoleColor(p1);
				printf("%c ",player0_capturedPieces.get(i));
			}
		}
		//print out captured pieces for player 1
		if(player1_capturedPieces.size() != 0)
		{
			setConsoleCursor(10, 5);
			setConsoleColor(p1);
			printf("Player 2 captured: ");
			for(int i = 0; i < player1_capturedPieces.size(); ++i)
			{
				setConsoleColor(p0);
				printf("%c ",player1_capturedPieces.get(i));
			}
		}
		//just moves the cursor to the bottom so it doesnt break up the board after the game ends
		setConsoleCursor(0,24);
		return true;

	}
	
	~Game()
	{
		//delete [] m_timer;
	}
};